package qimao.com.edu.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import qimao.com.edu.DTO.ChapterDTO;
import qimao.com.edu.dao.BookDao;
import qimao.com.edu.dao.ChaptersDao;
import qimao.com.edu.dao.impl.BookDaoImpl;
import qimao.com.edu.dao.impl.ChaptersDaoImpl;
import qimao.com.edu.entity.Chapters;
import qimao.com.edu.entity.Works;
import qimao.com.edu.service.ChaptersService;
import qimao.com.edu.service.NovelService;

public class ChaptersServiceImpl implements ChaptersService {
	BookDao bookDao = new BookDaoImpl();
	ChaptersDao  chaptersDao  = new ChaptersDaoImpl();

	@Override
	public ChapterDTO getChapterDetail(Integer bookId,Integer chapterNum) {
		Chapters chapters = bookDao.selectChapter(bookId, chapterNum);
//		ChapterDTO chapterDTO = bookDao.selectChapter(bookId, chapterNum);
		NovelService novelService = new NovelServiceImpl();
		String content = novelService.readNovelContent(chapters.getContentPath());
		ChapterDTO chapterDTO = new ChapterDTO();
		
		chapterDTO.setBookId(bookId);
		chapterDTO.setChapterNum(chapterNum);
		chapterDTO.setChapterName(chapters.getChapterTitle());
		chapterDTO.setContent(content);
		chapterDTO.setUpdateTime(chapters.getUpdate_time());
		chapterDTO.setWordCount(chapters.getWordCount());
		
		return chapterDTO;
	}
	
	@Override
	public boolean deleteChapter(int workId, int chapterId) {
		return chaptersDao.deleteChapter(workId, chapterId);
	}


	@Override
	public int addChapter(Chapters chapters,int bookId) {
		int result = 0;
		if(chaptersDao.addChapter(chapters) == 1) {
			Works works = new Works();
			works = bookDao.selectWorks(bookId);
			result = chaptersDao.updateWork(works);
		}
		return result;
	}
}
